import axios from 'axios'
import store from '@/store'
import {message} from 'ant-design-vue'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 50000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    return config
  },
  error => {
    // do something with request error
    console.log('接口异常', error) // for debug
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    if (response.status !== 200) {
      // Message({
      //   message: res.message || 'Error',
      //   type: 'error',
      //   duration: 5 * 1000
      // })
      return Promise.reject(new Error(response.message || 'Error'))
    } else {
      return response.data || true
    }
  },
  error => {
    let {status} = error.response;
   if(status === 401) {
     message.error('登录失效，请重新登录');
     store.commit('user/loginOut');
   }
    console.log(error, status);

    return error;
  }
)

export default service
